package com.sky.controller.user;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrdersPageQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/order")
public class OrderController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @PostMapping("/submit")
    public Result submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) throws Exception {
        OrderSubmitVO orderSubmitVO = ordersService.submit(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }
    @PutMapping("/payment")
    public Result payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) {
        String estimatedDeliveryTime = ordersService.payment(ordersPaymentDTO);
        //清空购物车
        shoppingCartService.clean();
        return Result.success(estimatedDeliveryTime);
    }

    @GetMapping("historyOrders")
    public Result<PageResult> historyOrder(Integer page, Integer pageSize,Integer status) {
        PageResult pageResult = new PageResult();
        pageResult = ordersService.page(page, pageSize,status);
        return Result.success(pageResult);
    }

    @GetMapping("/orderDetail/{id}")
    public Result<OrdersPageQueryVO> orderDetail(@PathVariable Long id) {
        OrdersPageQueryVO ordersPageQueryVO = ordersService.orderDetail(id);
        return Result.success(ordersPageQueryVO);
    }
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable Long id) {
        ordersService.cancel(id);
        return Result.success();
    }
    @PostMapping("repetition/{id}")
    public Result repetition(@PathVariable Long id) {
        ordersService.repetition(id);
        return Result.success();
    }
}
